package com.thunder.soft;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.IBinder;

import java.io.IOException;

public class MusicService extends Service {
    MediaPlayer mediaPlayer;
    MusicReceiver musicReceiver;

    public MusicService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = new MediaPlayer();
        musicReceiver=new MusicReceiver();
        IntentFilter filter=new IntentFilter();
        filter.addAction("CONTROL");
        registerReceiver(musicReceiver,filter);

        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                mediaPlayer.reset();
                try {
                    AssetManager assets = getAssets();
                    AssetFileDescriptor afd = assets.openFd("Dream_It_Possible.mp3");
                    mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
                    mediaPlayer.prepare();
                    mediaPlayer.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public class MusicReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals("CONTROL")) {
                int control = intent.getIntExtra("control", -1);
                switch (control) {
                    case 0:
                        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                            mediaPlayer.pause();
                        }
                        break;
                    case 1:
                        if (mediaPlayer.getCurrentPosition() == 0) {
                            mediaPlayer.reset();
                            try {
                                AssetManager assets = getAssets();
                                AssetFileDescriptor afd = assets.openFd("Dream_It_Possible.mp3");
                                mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
                                mediaPlayer.prepare();
                                mediaPlayer.start();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        } else {
                            mediaPlayer.start();
                        }
                        break;
                }

            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(musicReceiver);
    }
}